<?php

namespace app\api\controller\v1;

use app\api\validate\Express as ExpressValidate;
use app\common\exception\Client;
use app\common\exception\Server;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Request;
use app\common\service\Express as ExpressService;
use think\response\Json;
use app\common\validate\PagingParameter;

class Express
{
    /**
     * 客户预约配送快递
     * @param Request $request
     * @return Json
     * @throws Client
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException|Server
     */
    public function reservation(Request $request): Json
    {
        $token = $request->header('token');
        if (!$token)throw new Client(223001,'缺少Token');
        // 1. 接收参数
        $params = $request->only(['tracking_number', 'note', 'address', 'mobile','appt_type']);
        // 2. 参数验证
        $validate = new ExpressValidate();
        if(!$validate->scene('create')->check($params))throw new Client(223001,$validate->getError());
        // 3. 调用服务层
        (new ExpressService())->reservation($params,$token);
        // 4. 返回结果
        return writeJson(200,[],'预约成功');
    }

    /**
     * 取件码预约快递
     * @param Request $request
     * @return Json
     * @throws Client
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws Server
     */
    public function codeReservation(Request $request): Json
    {
        $token = $request->header('token');
        if (!$token)throw new Client(223001,'缺少Token');
        // 1. 接收参数
        $params = $request->only(['company_name', 'company_code', 'pickup_code', 'note','address', 'mobile','appt_type']);
        // 2. 参数验证
        $validate = new ExpressValidate();
        if(!$validate->scene('codeReserv')->check($params))throw new Client(223001,$validate->getError());
        // 3. 调用服务层
        (new ExpressService())->codeReservation($params,$token);
        // 4. 返回结果
        return writeJson(200,[],'预约成功');
    }

    /**
     * 客户分页查询快递
     * @param Request $request
     * @return Json
     * @throws Client
     */
    public function list(Request $request): Json{
        $token = $request->header('token');
        if (!$token)throw new Client(223001,'缺少Token');
        // 1. 接收参数
        $params = $request->only(['status','page','pagesize']);
        // 2. 参数验证
        $validate = new PagingParameter();
        if(!$validate->check($params))throw new Client(223001,$validate->getError());
        // 3. 调用服务层
        $res = (new ExpressService())->list($params,$token);
        // 4. 返回结果
        return writeJson(200,$res,'查询成功');
    }

    /**
     * 客户签收快递
     * @param Request $request
     * @return Json
     * @throws Client
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function signIn(Request $request): Json{
        // 1. 接收参数
        $token = $request->header('token');
        if (!$token)throw new Client(223001,'缺少Token');
        $params = $request->only(['id']);
        //验证参数
        $validate = new ExpressValidate();
        if(!$validate->scene('signIn')->check($params))throw new Client(223001,$validate->getError());
        //调用服务层，更新快递状态
        ExpressService::signIn($params,$token);
        //返回结果
        return writeJson(200,[],'签收成功');
    }

    /**
     * 获取快递公司与时间段信息
     * @param Request $request
     * @return Json
     */
    public function getCompanyAndTime(Request $request): Json{
        $result = (new ExpressService())->getCompanyAndTime();
        return writeJson(200,$result,'获取成功');
    }
}